package wechat.weblogin;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;

import de.robv.android.xposed.IXposedHookLoadPackage;
import de.robv.android.xposed.XC_MethodHook;
import de.robv.android.xposed.XposedBridge;
import de.robv.android.xposed.callbacks.XC_LoadPackage;

import static de.robv.android.xposed.XposedHelpers.findAndHookMethod;
import static de.robv.android.xposed.XposedHelpers.findClass;
import static de.robv.android.xposed.XposedHelpers.findMethodBestMatch;

/**
 * Created by xuxingcan on 16-11-20.
 */
public class XposedEntry implements IXposedHookLoadPackage {
    private String loginUuid;
    private Handler mHandler;
    private BroadcastReceiver broadcastReceiver;
    private Activity mUIActivity;

    @Override
    public void handleLoadPackage(final XC_LoadPackage.LoadPackageParam loadPackageParam) throws Throwable {
        if (!loadPackageParam.packageName.contains("tencent.mm")) {
            return;
        }
        try {
            hookLauncherUI(loadPackageParam.classLoader);
            findAndHookMethod("android.app.Activity", loadPackageParam.classLoader, "onCreate", Bundle.class, new XC_MethodHook() {
                @Override
                protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
                    super.beforeHookedMethod(param);
                    final Activity activity = (Activity) param.thisObject;
                    if (loginUuid != null && activity.getClass().getName().contains("scanner.ui.BaseScanUI")) {
                        if (mHandler == null) {
                            mHandler = new Handler(Looper.getMainLooper());
                        }
                        mHandler.postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                try {
                                    findMethodBestMatch(activity.getClass(), "a", String.class, int.class, int.class, int.class, findClass("com.tencent.mm.plugin.scanner.util.e$a", loadPackageParam.classLoader))
                                            .invoke(activity, "https://login.weixin.qq.com/l/" + loginUuid, 0, 19, 5, null);
                                } catch (Exception e) {
                                    XposedBridge.log(e);
                                }
                                loginUuid = null;
                            }
                        }, 1000);
                    }
                }
            });
        } catch (Exception exception) {
            XposedBridge.log(exception);
        }
    }

    private void hookLauncherUI(ClassLoader loader) {
        findAndHookMethod("com.tencent.mm.ui.LauncherUI", loader, "onCreate", Bundle.class, new XC_MethodHook() {
            @Override
            protected void afterHookedMethod(MethodHookParam param) throws Throwable {
                super.afterHookedMethod(param);
                mUIActivity = (Activity) param.thisObject;
                broadcastReceiver = new BroadcastReceiver() {
                    @Override
                    public void onReceive(Context context, Intent intent) {
                        XposedBridge.log("onReceive");
                        if ("WebLoginUUID".equals(intent.getAction())) {
                            Intent newIn = new Intent();
                            newIn.setClassName(mUIActivity.getPackageName(), "com.tencent.mm.plugin.scanner.ui.BaseScanUI");
                            mUIActivity.startActivity(newIn);
                            loginUuid = intent.getStringExtra("uuid");
                            XposedBridge.log("loginUuid = " + loginUuid);
                        }
                    }
                };
                mUIActivity.registerReceiver(broadcastReceiver, new IntentFilter("WebLoginUUID"));
            }
        });
        findAndHookMethod("com.tencent.mm.ui.LauncherUI", loader, "onDestroy", new XC_MethodHook() {
            @Override
            protected void afterHookedMethod(MethodHookParam param) throws Throwable {
                super.afterHookedMethod(param);
                if (broadcastReceiver != null) {
                    mUIActivity.unregisterReceiver(broadcastReceiver);
                }
            }
        });
    }
}
